﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Estsh.Core.Base;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Services;
using Estsh.Core.Repositories;
using Estsh.Core.Models;
using Newtonsoft.Json.Linq;
using Estsh.Core.Model.EnumUtil;
using static Estsh.Core.Model.EnumUtil.WmsEnumUtil;

namespace Estsh.Core.Wms.Services
{
    public class OffLineProInStockService : BaseService<BaseEntity>, IOffLineProInStockService
    {
        private readonly IOffLineProInStockRepository repository;
        public OffLineProInStockService(IOffLineProInStockRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        /// <summary>
        /// 线外生产入库
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="cartonNo"></param>
        /// <param name="locateName"></param>
        /// <returns></returns>
        public string CheckOffLineProInStock(string loginId, string cartonNo, string locateName,string groupNo)
        {
            //判断箱条码是否可用
            SysStock cartonInfo = repository.CheckCartonNoStatus(cartonNo);

            if (cartonInfo == null)
            {
                return "[" + cartonNo + "]:该条码不可用，请检查!";
            }
            if (cartonInfo.Enabled.Equals("N"))
            {
                return "[" + cartonNo + "]:箱条码已冻结，无法操作!";
            }
            if (cartonInfo.Status != (int)StockStatus.WAITING_INSTOCK)
            {
                return "[" + cartonNo + "]:不是待入库状态，请检查!";
            }

            //获取库位表库位与绑定零件信息
            List<SysLocate> sysLocates = repository.CheckLocateStatus(locateName);
            if (sysLocates.Count == 0)
            {
                return "[" + locateName + "]:该库位不存在，请检查!";
            }

            if (cartonInfo.ErpWarehouse != sysLocates[0].ErpWarehouse)
            {
                return "入库不允许跨库存地!";
            }

            List<SysStock> UseStocks = repository.CheckUseStockCapacity(locateName); //当前库位已经使用库存列表
            decimal QtySum = UseStocks.Sum(a => a.Qty);//已经使用库存总量


            //入库
            SysStock sysStock = repository.CheckCartonNoStatus(cartonNo);

            if (sysLocates[0].LocateCapacity > QtySum + sysStock.Qty)
            {
                //有库容
                if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
                {
                    // TODO 此处需要重新写逻辑
                    if (sysLocates.Where(a => a.PartId == cartonInfo.PartId).Count() > 0)
                    {
                        //基础库位正确 ，有库容，正常入库
                        List<SysStock> sysStocks = new List<SysStock>();
                        sysStocks.Add(sysStock);
                        return repository.UpdateStockStatus(sysStocks, sysLocates, (int)WmsEnumUtil.StockStatus.INSTOCKED, (int)WmsEnumUtil.TransType.INSTOCK, loginId, groupNo);
                    }
                    else
                    {
                        return "库位[" + locateName + "]与条码零件[" + cartonInfo.PartNo + "]不匹配！";
                    }
                }
                if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)
                {
                    //机动库位正确 ，有库容，正常入库
                    if (UseStocks.Where(a => a.PartId == cartonInfo.PartId).Count() >= 0)
                    {
                        //基础库位正确 ，有库容，正常入库
                        List<SysStock> sysStocks = new List<SysStock>();
                        sysStocks.Add(sysStock);
                        return repository.UpdateStockStatus(sysStocks, sysLocates, (int)WmsEnumUtil.StockStatus.INSTOCKED, (int)WmsEnumUtil.TransType.INSTOCK, loginId, groupNo);
                    }
                    else
                    {
                        return "库位[" + locateName + "]与条码零件[" + cartonInfo.PartNo + "]不匹配！";
                    }
                }
                if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.COMBINATION_LOCATE)
                {
                    //动态组合库库位正确 ，有库容，正常入库
                    List<SysStock> sysStocks = new List<SysStock>();
                    sysStocks.Add(sysStock);
                    return repository.UpdateStockStatus(sysStocks, sysLocates, (int)WmsEnumUtil.StockStatus.INSTOCKED, (int)WmsEnumUtil.TransType.INSTOCK, loginId, groupNo);
                }
                return "[" + locateName + "]:库位类型异常，请检查！";
            }
            else
            {
                return "[" + locateName + "]:该库位容量不足，请检查！";
            }

        }


        public List<WmsInstock> ShowOffLineProInStockList()
        {
            return repository.GetOffLineOrderList();
        }

        public string CheckWmsInstockStatus(string loginId, string orderNo)
        {
            return repository.CheckWmsInstockStatus(loginId, orderNo);
        }

        public List<SysStock> GetInstock(string orderNo)
        {
            return repository.GetInstock(orderNo);
        }
    }
}
